package com.blueseaapps;

import java.util.HashMap;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.blueseaapps.data.PercentileTable;
import com.blueseaapps.data.ScoreKeeper;

public class FinalScoreActivity extends Activity{
	//private Button btnPostFb;
	private Button btnRate;
	private Button btnFinish;
	
	private TextView totalQuestions;
	private TextView correctQuestions;
	private TextView correctQuestionsPercent;
	private TextView percentile;
	private TextView grade;

	private ScoreKeeper score;
	
	public static final String INTENT_EXTRA_SCORE = "com.unknown.FinalScoreActivity.Total";
	public static final String GRADE_SILVER = "Silver";
	public static final String GRADE_GOLD = "Gold";
	public static final String GRADE_DIAMOND = "Diamond";
	
    private final static String APP_PNAME = "com.unknown";

	// VarunCode
	private HashMap<Integer, PercentileTable> gradeMap = new HashMap<Integer, PercentileTable>();
	
	public FinalScoreActivity() {
		gradeMap.put(0, new PercentileTable(0,GRADE_SILVER));
		gradeMap.put(1, new PercentileTable(5,GRADE_SILVER));
		gradeMap.put(2, new PercentileTable(10,GRADE_SILVER));
		gradeMap.put(3, new PercentileTable(15,GRADE_SILVER));
		gradeMap.put(4, new PercentileTable(20,GRADE_SILVER));
		gradeMap.put(5, new PercentileTable(25,GRADE_SILVER));
		gradeMap.put(6, new PercentileTable(30,GRADE_SILVER));
		gradeMap.put(7, new PercentileTable(35,GRADE_SILVER));
		gradeMap.put(8, new PercentileTable(40,GRADE_SILVER));
		gradeMap.put(9, new PercentileTable(45,GRADE_SILVER));
		gradeMap.put(10, new PercentileTable(50,GRADE_SILVER));
		gradeMap.put(11, new PercentileTable(58,GRADE_GOLD));
		gradeMap.put(12, new PercentileTable(65,GRADE_GOLD));
		gradeMap.put(13, new PercentileTable(72,GRADE_GOLD));
		gradeMap.put(14, new PercentileTable(78,GRADE_GOLD));
		gradeMap.put(15, new PercentileTable(84,GRADE_GOLD));
		gradeMap.put(16, new PercentileTable(88,GRADE_DIAMOND));
		gradeMap.put(17, new PercentileTable(92,GRADE_DIAMOND));
		gradeMap.put(18, new PercentileTable(95,GRADE_DIAMOND));
		gradeMap.put(19, new PercentileTable(98,GRADE_DIAMOND));
		gradeMap.put(20, new PercentileTable(100,GRADE_DIAMOND));
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.final_score);
		btnFinish = (Button)findViewById(R.id.btn_finish_app);
		
		totalQuestions = (TextView)findViewById(R.id.val_ttl_ques);
		correctQuestions = (TextView)findViewById(R.id.val_corr_ques);
		correctQuestionsPercent = (TextView)findViewById(R.id.val_corr_ques_page);
		percentile = (TextView)findViewById(R.id.val_percentile);
		grade = (TextView)findViewById(R.id.val_grade);
		
		score = (ScoreKeeper) getIntent().getSerializableExtra(INTENT_EXTRA_SCORE);

		if(score != null){
			int totalQues = score.getNumOfQuestionsAttempted();
			int corrQues = score.getNumOfQuestionsCorrectlyAnswered();
			totalQuestions.setText(String.valueOf(totalQues));
			correctQuestions.setText(String.valueOf(corrQues));
			
			percentile.setText(String.valueOf(gradeMap.get(corrQues).percentile)+"%ile");
			grade.setText(gradeMap.get(corrQues).grade);

			if(totalQues > 0){
				correctQuestionsPercent.setText(String.valueOf(corrQues*100/totalQues)+"%");
			}
		}
		

		btnRate = (Button)findViewById(R.id.btn_rate_app);
//		btnPostFb = (Button)findViewById(R.id.btn_post_fb);
//		
//		btnPostFb.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				postToFbWall();
//			}
//		});
//		
		btnRate.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try{
					startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + APP_PNAME)));

				}catch(ActivityNotFoundException anfe){
					
				}
				finish();
			}
		});
		
		btnRate.setVisibility(View.GONE);
		
		btnFinish.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	private void postToFbWall() {
//		String msg = "Played quiz...";
//		Log.d("Tests", "Testing graph API wall post");
//        try {
//               String response = mFacebook.request("me");
//               Bundle parameters = new Bundle();
//               parameters.putString("message", msg);
//               parameters.putString("description", "test test test");
//               response = mFacebook.request("me/feed", parameters, 
//                       "POST");
//               Log.d("Tests", "got response: " + response);
//               if (response == null || response.equals("") || 
//                       response.equals("false")) {
//                  Log.v("Error", "Blank response");
//               }
//        } catch(Exception e) {
//            e.printStackTrace();
//        }
	}
	
}
